feat(builtins): add strings.split_n; fix split error-message name - #798
Open
Anand Krishnamoorthi (anakrish) wants to merge 2 commits into
Open
feat(builtins): add strings.split_n; fix split error-message name#798Anand Krishnamoorthi (anakrish) wants to merge 2 commits into
Anand Krishnamoorthi (anakrish) wants to merge 2 commits into
Conversation
strings.split_n splits a string into at most n pieces (positive n) or returns the last |n| pieces from a full split (negative n), using str::splitn for correct remainder semantics. Also fixes the long-standing typo in split() where the function registered its name as "replace" instead of "split", which produced misleading error messages (e.g. when split_n delegates to split for the negative-n tail path). - Register strings.split_n with arity 3 - Positive n: use str::splitn; handle empty-delimiter char-by-char with remainder to match OPA semantics - Negative n: full split, then take the last |n| pieces - Non-integer n in non-strict mode returns undefined (strict: error) - n == 0 returns empty array; n overflowing i64 positive treated as no limit (full split) - enforce_limit() on each piece to bound memory growth - Fix split() name string: "replace" -> "split" - Add YAML regression test covering all major code paths Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Fix fallback for n > i64::MAX: pass only first two args to split() instead of
all three; split() expects exactly 2 operands and ensure_args_count was rejecting
the extra one.
- Add enforce_limit() in empty-delimiter branch to prevent unbounded memory growth:
call it in the per-character map closure and before the final remainder push.
- Add YAML tests: split_n("abcd", "", 3) → ["a","b","cd"] (remainder)
and split_n("a,b,c", ",", 1) → ["a,b,c"] (n=1 returns whole string).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
strings.split_nand fixes a typo insplit().strings.split_n (new builtin, arity 3):
Bug fix — split(): name was registered as "replace" instead of "split", producing misleading error messages. Required for split_n negative-n path correctness.
docs/builtins.md: added strings.count and strings.split_n entries.
Tests: new tests/interpreter/cases/builtins/strings/split_n.yaml covering positive limit, exact limit, limit-exceeds-parts, zero limit, negative limit tail, empty string, non-integer n (undefined), wrong arg count.
Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com